package com.irdstudio.apicenter.console.controller.Platform;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppVersion;
import com.irdstudio.apicenter.frame.core.jdbc.JdbcBaseDAO;
import com.irdstudio.apicenter.frame.core.jdbc.container.DataList;
import com.irdstudio.apicenter.frame.core.jdbc.container.PageInfo;
import com.irdstudio.apicenter.frame.core.jdbc.tablemodel.ITableModelDAO;
import com.irdstudio.apicenter.frame.util.BeanUtils;

/**
 * APP版本管理
 * 
 * @author zs.ligm
 * @version 1.0
 * @date 2015-07-24
 */
@Controller
@RequestMapping(value = "/appVersion")
public class AppVersionManagerAction extends MacmsBaseAction {

	/**
	 * APP版本列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appVersionList")
	public String appVersionList(HttpServletRequest request) throws Exception {

		// 获取所有参数
		super.getAllRequestData(request);
		String appId = (request.getParameter("appid") == null) ? "" : request
				.getParameter("appid");
		String appName = (request.getParameter("appName") == null) ? ""
				: request.getParameter("appName");
		// 获取分页参数信息
		PageInfo pageInfo = super.getPageInfo();
		// 查询条件放入上下文中
		request.setAttribute("appId", appId);
		request.setAttribute("appName", appName);
		// 查询数据并放入到上下文中
		ITableModelDAO dao = (ITableModelDAO) BeanUtils
				.getBean("tableModelDAO");
		// DataList dataList =
		// dao.queryList("select * from app_version order by app_version_date desc",
		// pageInfo, null);
		DataList dataList = dao
				.queryList(
						"select a.*,b.app_name from app_version a left join app_base_info b on (a.app_id=b.app_id) where a.app_id = '"
								+ appId
								+ "' order by a.app_version_date desc",
						pageInfo, null);
		super.context.addDataList("avList", dataList);
		super.context.addPageInfo("avList", pageInfo);

		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);

		return "admin/versionList";
	}

	// 新增时初始化
	@RequestMapping(value = "JumpAddVersion")
	public String addVersion(HttpServletRequest request) throws Exception {

		// 获取所有参数
		super.getAllRequestData(request);

		// 查询数据并放入到上下文中
		/*
		 * ITableModelDAO dao = (ITableModelDAO)
		 * BeanUtils.getBean("tableModelDAO"); DataList dataList =
		 * dao.queryList(
		 * "select app_id,app_name, APP_VERSION_ID from app_base_info", null,
		 * null); super.context.addDataList("appList", dataList);
		 */

		String sql = " select app_id,app_name, APP_VERSION_ID from app_base_info";
		JdbcBaseDAO baseDao = BeanUtils.getBean(JdbcBaseDAO.class);
		request.setAttribute("verList", baseDao.findMapList(sql, null));
		// 将数据上下文放到属性中
		request.setAttribute("context", super.context);

		return "admin/addVersion";
	}

	@RequestMapping(value = "addVersion")
	public void addVersion(@ModelAttribute("appVersion") AppVersion appVersion,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		JdbcBaseDAO baseDao = BeanUtils.getBean(JdbcBaseDAO.class);
		flag = baseDao.insert(
				"insert into app_version  values(?,?,?,?,?,?,?,?)", appVersion
						.getAppId(), appVersion.getAppVersionId(), appVersion
						.getAppLastVersionId(), appVersion.getAppVersionDate(),
				appVersion.getAppUpdateUrl(), appVersion.getAppFileSize(),
				appVersion.getAppFileHash(), appVersion.getIsMustUpdate());
		flag = baseDao
				.update(
						"update  app_base_info  set  app_version_id=?  where  app_id=?",
						appVersion.getAppVersionId(), appVersion.getAppId());
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}
	
	/**
	 * 查看应用版本的二维码
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "viewQrcode")
	public String viewQrcode(HttpServletRequest request) throws Exception {

		return "admin/viewQrcode";
	}

	@RequestMapping(value = "deleteVersion")
	public void deleteVersion(
			@ModelAttribute("appVersion") AppVersion appVersion,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		String appId = request.getParameter("appId");
		String versionId = request.getParameter("versionId");
		JdbcBaseDAO baseDao = BeanUtils.getBean(JdbcBaseDAO.class);
		flag = baseDao
				.delete(
						"delete from app_version where app_id=?  and  APP_VERSION_ID=?",
						appId, versionId);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

}
